package com.tjetc.view;

import com.tjetc.domain.Customer;
import com.tjetc.domain.CustomerMessage;
import com.tjetc.domain.Order;
import com.tjetc.domain.Vehicle;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.MessageService;
import com.tjetc.service.OrderService;
import com.tjetc.service.VehicleService;
import com.tjetc.utils.DateJudge;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class VehicleCustomerView {
    private OrderService orderService = ClientProxy.getProxyInstance(OrderService.class);
    private VehicleService vehicleService = ClientProxy.getProxyInstance(VehicleService.class);
    private MessageService messageService = ClientProxy.getProxyInstance(MessageService.class);
    private static Scanner sc = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("🥰🥰~~~~~~车辆查询与租赁页面~~~~~~🥰🥰");
            System.out.println("1.可租车辆列表");
            System.out.println("2.提交租车订单");
            System.out.println("0返回上一级");
            System.out.println("请输入你的选择");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    commitOrder();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误请重新输入");
            }
        }
    }

    /**
     * 提交订单
     * @return
     */
    private int commitOrder() {
        //确保用户已经进行实名认证
        Customer customer = AppData.customer;
        Integer customerId = customer.getCustomerId();
        Result result1 = messageService.findById(customerId);
        if (result1.getCode() == 200) {
            //列出所有车辆
            list();
            System.out.println("请输入你选择的车辆");
            //输入车辆id
            int vid = sc.nextInt();
            //设置时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            LocalDateTime startTime = null;
            LocalDateTime endTime = null;
            //输入开始的时间
            try {
                System.out.println("请输入你开始的时间(例:20250311082300)");
                while (true) {
                    String start = sc.next();
                    if (!DateJudge.dateJudge(start)) {
                        System.out.println("输入时间出了问题,请重新输入");
                        continue;
                    }
                    //输入时间是否比当前时间多一个小时
                    startTime = LocalDateTime.parse(start, formatter);
                    if (ChronoUnit.HOURS.between(LocalDateTime.now(), startTime) >= 1) {
                        if (ChronoUnit.DAYS.between(LocalDateTime.now(),startTime)<=7){
                            break;
                        }
                        System.out.println("开始时间必须在七天之内");
                    }
                    System.out.println("输入的时间必须比当前时间大于一小时");
                }
                System.out.println("请输入结束的时间(例:20250311082300)");
                String end = "";
                //判断时间是否符合规矩和最少租一天
                while (true) {
                    end = sc.next();
                    if (!DateJudge.dateJudge(end)){
                        System.out.println("输入的时间出了问题,请重新输入");
                        continue;
                    }
                    endTime = LocalDateTime.parse(end, formatter);
                    if (ChronoUnit.DAYS.between(startTime, endTime) >= 1) {
                        break;
                    } else {
                        System.out.println("你输入的时间太短,最低需要租1天");
                    }
                }
            } catch (Exception e) {
                System.out.println("输入有误!!!!!");
                return 0;
            }
            //获取订单id
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Order order = new Order();
            Random random = new Random();
            order.setOrderId(simpleDateFormat.format(new Date()) + "" + (random.nextInt(100000, 999999)));
            order.setCustomerId(AppData.customer.getCustomerId());
            order.setVehicleId(vid);
            order.setRentalStartDate(startTime);
            order.setRentalEndDate(endTime);
            order.setOrderStatus(0);
            order.setTotalCost(null);
            order.setOrderDate(LocalDateTime.now());
            Result result = orderService.commit(order);
            System.out.println(result.getMsg());
            return 0;
        } else {
            System.out.println("请先进行实名认证!");
            return 0;
        }
    }

    /**
     * 列出所有车辆
     */
    private void list() {
        //根据条件查询
        System.out.println("是否选择条件筛选1是/2不是");
        int choose = sc.nextInt();
        String model = null;
        double min = 0;
        double max = 0;
        //根据条件查询
        if (choose == 1) {
            System.out.println("请输入想要选的的车型");
            model = sc.next();
            System.out.println("请输入您能接受最小的价格");
            min = sc.nextDouble();
            System.out.println("请输入您能接受的最大的价格");
            max = sc.nextDouble();
        }
        Result result = vehicleService.listBySome(model, min, max);
        //查询车辆
        List<Vehicle> list = (List<Vehicle>) result.getData();
        //判断车辆是否未空
        if (list == null || list.size() == 0) {
            System.out.println("没有适合的车辆,再看看吧\uD83D\uDE18\uD83D\uDE18\uD83D\uDE18");
        }else {
            System.out.println("可租的车:");
            for (Vehicle vehicle : list) {
                System.out.println("车辆的id:"+vehicle.getVehicleId()+",车辆的型号:"+vehicle.getModel()+
                        ",车辆的颜色:"+vehicle.getColor()+
                        ",车牌号:"+vehicle.getLicensePlate()+
                        ",日租金:"+vehicle.getDailyRentalRate());
            }
        }
    }
}
